<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

use common\models\Admin;

/**
 * Site controller
 */
class AdminController extends Controller
{
    public $enableCsrfValidation = false;//开启防止crf攻击
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error','logindo','index','logout','work-console','edit-user-password','edit-user-info'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index','login','logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                   // 'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
   // $userInfo=\Yii::$app->session->get('userInfo');
        $userId=getUserId();
        $userInfo=Admin::find()->joinWith('roles')->joinWith('departments')->where(['al_admin.id'=>$userId])->asArray()->one();
//      var_dump($userInfo);
//      die;
        return $this->renderPartial('index',$userInfo);
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        $userInfo=\Yii::$app->session->get('userInfo');
        if(empty($userInfo)){
            return $this->render('login');
        }else{
            return $this->redirect(['admin/index']);
        }

       /* if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }*/
    }
    public function actionLogindo(){
       // $this->layout='base';
        $params=Yii::$app->request->post();
        $userName=$params['username'];
        $password=$params['password'];
        if(empty($username)){

        }
        if(empty($password)){

        }
        $checkLogin=Admin::verifyAdminUser($userName,$password);
        if($checkLogin['errcode']==0){
            ajaxReturn($checkLogin);
           // return $this->redirect(['admin/index']);
        }else{
            ajaxReturn($checkLogin);
           // return $this->render('login');
        }
        /*var_dump($adminObj->userName);
        var_dump(Yii::$app->request->post());
        die;*/
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
       \Yii::$app->session->removeAll();
        return $this->redirect(['admin/login']);
    }
    public function actionWorkConsole(){
        return $this->render('work-console');
    }
    /**
     * 修改自己的密码
     */
    public function actionEditUserPassword(){
        $params=Yii::$app->request->post();
        $oldPwd=getData($params['oldPwd'],'请填写旧密码');
        $newPwd=getData($params['newPwd'],'请填写新密码');
        $confirmPwd=getData($params['confirmPwd'],'请填写确认密码');
        if($newPwd!=$confirmPwd){
            returnResult(100035,'两次密码不一致请重新填写');
        }

        $userId=getUserId();
        $checkOldPwd=Admin::findOne(['id'=>$userId,'password'=>md5($oldPwd)]);
        if(empty($checkOldPwd)){
            returnResult(100037,'旧密码错误');
        }
        $users=Admin::findOne(['id'=>$userId]);
        $users->password=md5($newPwd);
        $doResult=$users->save();

        if($doResult){
            returnResult(0,'修改密码成功');
        }else{
            returnResult(100038,'修改密码失败');
        }
    }
    /**
     * 修改个人邮箱和手机号码
     */
    public function actionEditUserInfo(){
        $params=Yii::$app->request->post();
        $email=getData($params['email'],'请填写邮箱');
        $cellPhone=getData($params['cellPhone'],'请填写手机号码');
        $userId=getUserId();
        $users=Admin::findOne(['id'=>$userId]);
        $users->email=$email;
        $users->cellPhone=$cellPhone;
        $doResult=$users->save();
        if($doResult){
            returnResult(0,'编辑成功');
        }else{
            returnResult(100039,'编辑失败');
        }
    }

}
